/*
 * This file or a portion of this file is licensed under the terms of
 * the Globus Toolkit Public License, found in file GTPL, or at
 * http://www.globus.org/toolkit/download/license.html. This notice must
 * appear in redistributions of this file, with or without modification.
 *
 * Redistributions of this Software, with or without modification, must
 * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
 * some other similar material which is provided with the Software (if
 * any).
 *
 * Copyright 1999-2004 University of Chicago and The University of
 * Southern California. All rights reserved.
 */

package edu.isi.pegasus.common.util;

import java.io.*;

/* DO NOT EDIT! This file is generated automatically */

/**
 * This class solely defines the version numbers of PEGASUS. The template
 * file will be substituted by ant during the built process to compile
 * in the built timestamp.<p>
 *
 * When using the class, the methods for the version number digits
 * will always return an integer. In order to obtain the full version
 * number, including any "release candidate" suffices, please prefer
 * the <code>toString</code> method over combining the separate version
 * integers yourself.
 *
 * @author Karan Vahi
 * @author Jens-S. VÃ¶ckler
 * @version $Revision: 5067 $
 * $Id: Version.in 5067 2012-03-01 00:27:48Z rynge $
 */
public class Version 
{
  /**
   * This constant defines the major version number.
   */
  public static final int MAJOR = 4;

  /**
   * This constant defines the minor version number.
   */
  public static final int MINOR = 0;

  /**
   * This constant defines the patch level version.
   */
  public static final int PLEVEL = 0;

  /**
   * Instance variable keeping track of the Singleton.
   */
  protected static Version c_instance = null;

  /**
   * C'tor: construct an element, which is empty anyway.
   */
  private Version()
  {
    // empty
  }

  /**
   * Accessor to the singleton.
   */
  public static Version instance()
  {
    if ( Version.c_instance == null ) 
      Version.c_instance = new Version();
    return Version.c_instance;
  }
  
  /**
   * Returns a string containing the concatenated version.
   * Note: This class may also return suffixes beyond the version.
   *
   * @return a concatenated string.
   */
  public String toString()
  {
    return ( Integer.toString(MAJOR) + '.' + Integer.toString(MINOR) + 
	     '.' + Integer.toString(PLEVEL) + "" ); // !! HERE !!
  }

  /**
   * Basename of the build stamp file.
   */
  public static final String STAMP_FILENAME = "stamp";

  /**
   * Determines the built as a time stamp.
   * @return the formatted time stamp of the built. 
   */  
  public String determineBuilt()
  {
    return "20120406095041Z";
  }

  /** 
   * Determines the build platform.
   * @return an identifier for the build platform.
   */
  public String determinePlatform()
  {
    return "x86_64_macos_10.6";
  }

  /**
   * Determines the built and architecture of the installation. These
   * are usually separated by a linear white-space.
   *
   * @return the formatted time stamp of the built, if available, and an
   * identifier for the architecture. An string indicating that the
   * build is unknown is returned on failure.
   */
  public String determineInstalled() 
  {
    String result = "unknown unknown";
    String pegasushome = System.getProperty( "pegasus.home" );
    if ( pegasushome != null ) {
      try {
	File stampfile = new File( pegasushome, STAMP_FILENAME );
	if ( stampfile.canRead() ) {
	  BufferedReader br = new BufferedReader( new FileReader(stampfile) );
	  String built = br.readLine();
	  br.close();
	  if ( built != null && built.length() > 0 ) result = built;
	}
      } catch ( IOException ioe ) {
	// ignore
      }
    }

    return result;
  }

  /**
   * Determines, if the compiled version and the installed version
   * match. The match is done by comparing the timestamps and
   * architectures.
   *
   * @return true, if versions match, false otherwise.
   */
  public boolean matches()
  {
    String s[] = determineInstalled().split("\\s+");
    return ( s.length >= 2 &&
	     s[0].equalsIgnoreCase( determineBuilt() ) &&
	     s[1].equalsIgnoreCase( determinePlatform() ) );
  }
}
